//
//  BaseSearchView.m
//  ShunLiBan
//
//  Created by ihope99 on 16/8/26.
//  Copyright © 2016年 iHope. All rights reserved.
//

#import "BaseSearchView.h"

@implementation BaseSearchView



-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        mView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, frame.size.height)];
        mView.layer.masksToBounds = YES;
        mView.layer.borderColor = KLineColor.CGColor;
        mView.layer.borderWidth = 0.5;
        mView.layer.cornerRadius = 5;
        mView.backgroundColor = [UIColor whiteColor];
        [self addSubview:mView];
        
        enlargeImage = [UIImage imageNamed:@"home_search_gray"];
        
        searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, (mView.frame.size.height-enlargeImage.size.height)/2.0, enlargeImage.size.width, enlargeImage.size.height)];
        searchImage.image = enlargeImage;
        [mView addSubview:searchImage];
        
        
        mSearchTextField = [[UITextField alloc]initWithFrame:CGRectMake(10+20, 0, mView.frame.size.width-10-20, mView.frame.size.height)];
        mSearchTextField.font  = [UIFont systemFontOfSize:13.];
        mSearchTextField.tintColor = KTinColor;
        mSearchTextField.textColor = KBorColor;
        [mSearchTextField setValue:KSubColor forKeyPath:@"_placeholderLabel.textColor"];
        mSearchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [mView addSubview:mSearchTextField];
        mSearchTextField.delegate = self;
        mSearchTextField.returnKeyType = UIReturnKeySearch;
        
      
    }
    return self;
}
-(void)becomeFirstResponder{
    [mSearchTextField becomeFirstResponder];
}
-(void)setEnabled:(BOOL)enabled{
    _enabled = enabled;
    mSearchTextField.enabled = enabled;
}
-(void)setFont:(UIFont *)font{
    _font = font;
    mSearchTextField.font = font;
}
-(void)setText:(NSString *)text{
    mSearchTextField.text = text;
}
-(NSString *)text{
    return mSearchTextField.text;
}
-(void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    mSearchTextField.textColor = textColor;
}
-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    mSearchTextField.placeholder = placeholder;
}
-(void)setInputAccessoryView:(UIView *)inputAccessoryView{
    _inputAccessoryView = inputAccessoryView;
    mSearchTextField.inputAccessoryView = inputAccessoryView;
}
-(void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    [mSearchTextField setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
}
-(void)setReturnKeyType:(UIReturnKeyType)returnKeyType{
    _returnKeyType = returnKeyType;
    mSearchTextField.returnKeyType = returnKeyType;
}



#pragma mark -  UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (_myDelegate && [_myDelegate respondsToSelector:@selector(textFieldShouldReturn:)]) {
        return [_myDelegate textFieldShouldReturn:textField];
    }
    return NO;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (_myDelegate && [_myDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        [_myDelegate textFieldDidBeginEditing:textField];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
